﻿using System.Linq;
using System.Collections.Generic;
using Codeblack.TfsScrumboard.Entities;
using Codeblack.TfsScrumboard.ViewModels;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Codeblack.TfsScrumboard.Extensions
{
    public static class ScrumboardExtensions
    {
        public static T GetFieldValue<T>(this WorkItem workItem, string fieldName)
        {
            T value = default(T);

            if (workItem.Fields.Contains(fieldName))
            {
                Field field = workItem.Fields[fieldName];
                if (field != null && field.Value != null & field.Value is T)
                {
                    value = (T)field.Value;
                }
            }

            return value;
        }
        public static void SetFieldValue<T>(this WorkItem workItem, string fieldName, T fieldValue)
        {
            if (workItem.Fields.Contains(fieldName))
            {
                Field field = workItem.Fields[fieldName];
                if (field != null)
                {
                    field.Value = fieldValue;
                }
            }
        }

        public static string GetFullIterationPath(this Project project, string backlogName)
        {
            return FindIterationPath(project.IterationRootNodes, backlogName);
        }
        private static string FindIterationPath(NodeCollection nodes, string backlogName)
        {
            if (nodes != null)
            {
                foreach (Node node in nodes)
                {
                    if (node.Name.ToLowerInvariant() == backlogName.ToLowerInvariant())
                    {
                        return node.Path;
                    }
                    else
                    {
                        string iterationPath = FindIterationPath(node.ChildNodes, backlogName);
                        if (!string.IsNullOrEmpty(iterationPath))
                        {
                            return iterationPath;
                        }
                    }
                }
            }

            return string.Empty;
        }

        public static BacklogViewModel[] ToViewModels(this IEnumerable<Backlog> backlogs)
        {
            return (from b in backlogs
                    select new BacklogViewModel()
                    {
                        Name = b.Name
                    }).ToArray();
        }
        public static WorkItemViewModel[] ToViewModels(this IEnumerable<Workitem> workitems)
        {
            return (from w in workitems
                    select new WorkItemViewModel()
                    {
                        WorkItemType = w.WorkItemType,
                        ID = w.ID,
                        Title = w.Title,
                        StoryPoints = w.StoryPoints,
                        Status = w.Status,
                        IterationPath = w.IterationPath,
                        AssignedTo = w.AssignedTo
                    }).ToArray();
        }
    }
}
